﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DetectScars.common
{
    internal class FileUtils
    {
        public static string PATH = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "data\\dataa");
        //加载指定文件夹下的文件按
        internal static string getFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                // 设置过滤器，例如只显示CSV文件
                Filter = "Files (*.bin)|*.bin"
            };
            openFileDialog.InitialDirectory = PATH;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // 获取选中文件的完整路径
                string filePath = openFileDialog.FileName;
                return filePath;
            }
            return string.Empty;
        }

        // 保存数据到文件
        internal static void SaveDataToFile(double[,] data, string filePath)
        {
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                writer.WriteLine(data.GetLength(0));
                writer.WriteLine(data.GetLength(1));
                for (int i = 0; i < data.GetLength(0); i++)
                {
                    for (int j = 0; j < data.GetLength(1); j++)
                    {
                        writer.Write(data[i, j] + " ");
                    }
                    writer.WriteLine();
                }
            }
        }

        internal static double[,] LoadDataFromFile(string filePath)
        {
            using (StreamReader reader = new StreamReader(filePath))
            {
                // 读取行数和列数
                int rows = int.Parse(reader.ReadLine());
                int cols = int.Parse(reader.ReadLine());

                // 创建数组
                double[,] data = new double[rows, cols];

                // 读取数据填充数组
                for (int i = 0; i < rows; i++)
                {
                    string[] values = reader.ReadLine().Split(' ');
                    for (int j = 0; j < cols; j++)
                    {
                        data[i, j] = double.Parse(values[j]);
                    }
                }
                return data;
            }


        }
    }
}
